//
//  CaiPuClassifyDetailViewController.m
//  KCZ_B段项目
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 Sky. All rights reserved.
//

#import "CaiPuClassifyDetailViewController.h"
#import "FitHeader.h"
#import "HTTPMethod.h"
#import "changeString.h"
#import <MJRefresh.h>
#import <MBProgressHUD.h>
#import "ZJHShakeDetailTableViewCell.h"
#import "ZJHShakeCaipuViewController.h"
@interface CaiPuClassifyDetailViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)MBProgressHUD *progressView;
@property(nonatomic, strong)NSMutableArray *dataArray;
@property(nonatomic, assign)NSInteger offset;
@end

@implementation CaiPuClassifyDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.title = [_tempDic objectForKey:@"Name"];
    
}
-(void)loadView
{
    [super loadView];
    _dataArray = [NSMutableArray array];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 104)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_dataArray removeAllObjects];
        _offset = 0;
        [self getData];
    }];
    _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _offset += 20;
        [self getData];
    }];
    [self createProgressView];
    [self getData];
}
//创建progressView
-(void)createProgressView
{
    _progressView = [[MBProgressHUD alloc]initWithView:self.view];
    _progressView.labelText = @"加载中...";
    _progressView.backgroundColor = [UIColor whiteColor];
    [_progressView setMode:MBProgressHUDModeIndeterminate];
    [self.view addSubview:_progressView];
    [_progressView show:YES];
}
-(void)getData
{
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
    NSInteger date = time;
    NSString *post1 = @"http://api.haodou.com/index.php?appid=2&appkey=9ef269eec4f7a9d07c73952d06b5413f&format=json&sessionid=1446462089764&vc=77&vn=5.4.0&loguid=0&deviceid=haodou359786058377939&uuid=087c7496ab334bd9babe6eb99709c4f8&channel=baidu_v540&method=Search.getList&virtual=&signmethod=md5&v=2&";
    NSString *post2 = [NSString stringWithFormat:@"timestamp=%ld&nonce=0.1695834918132666&appsign=1cee6e94957b612de7cbb4a2284c6115", date];
    NSString *postString = [NSString stringWithFormat:@"%@%@", post1, post2];
    NSString *bodyString = [NSString stringWithFormat:@"limit=20&scene=t1&tagid=%ld&uuid=087c7496ab334bd9babe6eb99709c4f8&offset=%ld", [[_tempDic objectForKey:@"Id"]integerValue], _offset];
    [HTTPMethod getDataByString:postString BodyDic:[changeString changeStringToDictionary:bodyString] WithDataBlock:^(id data) {
        NSDictionary *dic = [data objectForKey:@"result"];
        NSArray *listArray = [dic objectForKey:@"list"];
        for (NSDictionary *dic in listArray) {
            ZJHShakeModel *model = [[ZJHShakeModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:model];
        }
        [_tableView reloadData];
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
        [_progressView removeFromSuperview];
    }];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    ZJHShakeDetailTableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[ZJHShakeDetailTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (_dataArray.count != 0) {
        cell.shakeModel = _dataArray[indexPath.row];
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80 * ht;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZJHShakeCaipuViewController *VC = [[ZJHShakeCaipuViewController alloc]init];
    VC.tempModel = _dataArray[indexPath.row];
    [self.navigationController pushViewController:VC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
